// by Xeno
private ["_i", "_element", "_vec", "_box_pos","_delete_box"];
if (!isServer) exitWith {};

// Checks if the box is far from the vehicle it belongs to of the box and then deletes it.
while {true} do {
	if (X_MP) then {waitUntil {sleep (1.012 + random 1);(call XPlayersNumber) > 0}};
	_boxes = ["d_ammo_boxes",[]] call XNetGetJIP;
	//diag_log Format["(%1)[%2]: %3 %4 at %5",diag_tickTime,"boxhandling_old.sqf","Boxes to check", count _boxes];
	for [ {_i = (count d_check_boxes - 1)}, {_i >= 0 }, {_i = _i - 1}] do {
		_element = d_check_boxes select _i;
		_vec = _element select 0;
		_box_pos = _element select 1;
		If (typename _vec == "STRING") then {
			// Here we are in the case of boxes dropped by air. We check that there are players nearby, otherwise we delete them.
			_delete_box = true;
			_entities = nearestObjects [_box_pos,["Man","LandVehicle","Air"],400];
			{
				if ((_x isKindOf "Air") Or (_x isKindOf "LandVehicle")) then {
					{
						if (isPlayer _x) exitWith { _delete_box = false; };
					} foreach crew _x;
					if (!_delete_box) exitWith { _delete_box = false; };
				} else {
					if (isPlayer _x) exitWith { _delete_box = false; };
				};
			} foreach _entities;
			if (_delete_box) then {
				if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 at %5 (%6)",diag_tickTime,"boxhandling_old.sqf","Airbox Delete", _i, _box_pos, (_box_pos call XfGPSCoords)]; };
				d_check_boxes set [_i, -1];
				If (_i < count (_boxes)) then {
					["d_r_box", (_boxes select _i)] call XNetCallEvent;
					_boxes set [_i, -1];
					_boxes = _boxes - [-1];
				} else {
					["d_r_box", [_box_pos]] call XNetCallEvent;
					if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 < %5",diag_tickTime,"boxhandling_old.sqf","Error: Index outside bounds", count _boxes, _i]; };
				};
				d_check_boxes = d_check_boxes - [-1];
			};
			sleep 0.01;
		} else {
			if (isNull _vec) then {
				if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 at %5",diag_tickTime,"boxhandling_old.sqf","Vehicle Not Found Removing box", _i,_box_pos]; };
				d_check_boxes set [_i, -1];
				["ammo_boxes",(["ammo_boxes",1] call XNetGetJIP) - 1] call XNetSetJIP;
				If (_i < count (_boxes)) then {
					["d_r_box", (_boxes select _i)] call XNetCallEvent;
					_boxes set [_i, -1];
					_boxes = _boxes - [-1];
				} else {
					["d_r_box", [_box_pos]] call XNetCallEvent;
					if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 < %5",diag_tickTime,"boxhandling_old.sqf","Error: Index outside bounds", count _boxes, _i]; };
				};
				sleep 0.1;
				d_check_boxes = d_check_boxes - [-1];
			} else {
				// Now we have a vehicle which dropped a box. We delete the corresponding box if the vehicle is too far.
				if (_vec distance _box_pos > 600) then {
					if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 for %5 (%6)",diag_tickTime,"boxhandling_old.sqf","Too Far Removing box", _i, str _vec, (_vec distance _box_pos)]; };
					d_check_boxes set [_i, -1];
					["ammo_boxes",(["ammo_boxes",1] call XNetGetJIP) - 1] call XNetSetJIP;
					If (_i < count (_boxes)) then {
						["d_r_box", (_boxes select _i)] call XNetCallEvent;
						_boxes set [_i, -1];
						_boxes = _boxes - [-1];
					} else {
						["d_r_box", [_box_pos]] call XNetCallEvent;
						if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4 < %5",diag_tickTime,"boxhandling_old.sqf","Error: Index outside bounds", count _boxes, _i]; };
					};
					sleep 0.1;
					d_check_boxes = d_check_boxes - [-1];
				};
			};
		};
	};
	["d_ammo_boxes",_boxes] call XNetSetJIP;
	sleep 45;
};